package code.structure.module.user
{
	import code.main.AppCommonData;
	import code.main.AppData;
	import code.main.core.mvc.YModelData;
	import code.structure.module.configs.ConfigModel;
	import code.structure.module.user.data.UserData;
	import code.untils.CommandNameConst;
	import code.untils.NetDataBackKeyConst;
	import code.untils.SystemFunction;
	
	import com.patience.mvc.context.Command;
	import com.patience.net.events.NetEvent;
	
	import flash.utils.getTimer;
	
	/**
	 *用户数据
	 * @author Yuri
	 */
	public class UserModel extends YModelData
	{
		private var _userData:UserData;
		private var _configModel:ConfigModel;
		private var _command:Command = new Command('');
		
		public function UserModel()
		{
			super();
			addEvtListener();
		}
		
		//添加侦听 接收用户基础数据
		private function addEvtListener():void
		{
			_net.addEventListener(NetDataBackKeyConst.USER_DATA, analysisData);
		}
		
		private function analysisData(e:NetEvent):void{
			AppData.iDelayTime = (getTimer() - AppData.iDelayTime)/1000;
			//SystemFunction.setServerTime(e.data.server_time,e.data.server_time_f, AppData.iDelayTime);
			SystemFunction.setServerTime(1407895524,'2014/08/13 04:05:24', AppData.iDelayTime);
			_configModel = (_context.commonData as AppCommonData).configModel;
			_userData = new UserData(dispatch,_configModel);
			_userData.analysisData(e.data.baseinfo);
			//_userData.analysisData(e.data.userdata);
			_userData.userBaseData.levelConfigData = _configModel.analysisLevel.getlevelConfigDataById(_userData.userBaseData.uLevel);
			_userData.levelHelpHandler();
			//抛出解析完毕
			dispatch(new Command(CommandNameConst.COMMAND_USERDATA_ANALY_COMPLETE));
		}
		
		/**
		 * 更新货币数量
		 * @param number 加多少
		 * @param type 哪一种货币类型
		 * @param isPop 当货币数量不够时是否弹窗提示 默认弹窗
		 * @param isSubtract 当货币数量足够时，是否扣除，默认扣除
		 * @return 返回货币是否足够 false 不够 true 够
		 * 
		 */		
		public function changeMoney(number:Number, type:int = 1, isPop:Boolean = true,isSubtract:Boolean = true):Boolean
		{
			return _userData.changeMoney(number,type,isPop,isSubtract);
		}
		
		
		/**
		 * 改变等级
		 * @param	value
		 */
		public function changeLevel(value:int = 1):void
		{
			_userData.changeLevel(value);
			//ProjectAssistManager.getIntance().triggerDataManager.analyActionTriggerTask(TaskTypeConst.TYPE_REACH_LEVEL, SpecialItemConst.LEVEL, _userData.userBaseData.uLevel, false);
			//_taskModel.checkAddNewTask();
		}
		
		/**
		 * 改变钻石值
		 * @param	value
		 */
		public function changeDiamond(value:int):void
		{
			_userData.changeDiamond(value);
		}
		
		/**
		 * 改变经验值
		 * @param	value
		 */
		public function changeExp(value:int):void
		{
			_userData.changeExp(value);
		}
		
		/**
		 * 改变金币值
		 * @param	value
		 */
		public function changeCoins(value:int):void
		{
			_userData.changeCoins(value);
		}
		/**
		 *更新能量值
		 * @param value
		 * @param limit 是否限制能量上限
		 */
		public function changeEnergy(value:int, limit:Boolean = true):int
		{
			return _userData.changeEnergy(value,limit);
		}
		/**
		 *改变庄园名称 
		 * @param s
		 * 
		 */		
		public function changeManorName(s:String):void
		{
			_userData.changeManorName(s);
		}
		/**
		 *改变魅力值
		 * @param s
		 * 
		 */		
		public function changeMagic(value:int):void
		{
			_userData.changeMagic(value);
		}
		/**
		 * 发送命令
		 * @param	cmd
		 */
		public function sendCommand(name:String, param:int = 0, target:int = 0):void
		{
			_command.cmdName = name;
			_command.data = param;
			_command.target = target;
			dispatch(_command);
		}
	}
}